package com.zhu.rpc.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.InetSocketAddress;

public class RpcServer {
    Logger logger = LoggerFactory.getLogger(RpcServer.class);
    public Future<Void> start(int port,String host){
        ServerBootstrap b = new ServerBootstrap();
        //TODO 可以使用Epoll 可以使用配置类
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(0);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(0);
        Class<? extends ServerChannel> channelClass = NioServerSocketChannel.class;
        RpcServerChannelInitializer rpcChannelInitializer = new RpcServerChannelInitializer();
        b.group(bossGroup,workerGroup)
                .channel(channelClass)
                .childHandler(rpcChannelInitializer);
        InetSocketAddress addr = new InetSocketAddress(port);
        if (host != null) addr=new InetSocketAddress(host,port);

        return b.bind(addr).addListener((future)->{
            if (future.isSuccess()){
                logger.info("Rpc服务端已启动 端口为{}",port);
            }else {
                logger.info("Rpc服务端启动失败 端口为{}！！！",port);
            }
        });
    }

    public void stop(){

    }

}
